// Copyright 2010 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.android.apps.moderator;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

/**
 * A list entry to represent a Google Moderator Series
 */
public class SeriesListEntry extends ModeratorListEntry {

  // Moderator's own ID for the series this list entry refers to
  private int seriesId;
  
  // Compact mode is where the series details should take up less screen space, e.g. the MRU list.
  private boolean compactMode;

  /**
   * Make a series list entry from the JSON data representing the object
   */
  public SeriesListEntry(JSONObject jsonObject, boolean compactMode) {
    super(jsonObject);
    this.compactMode = compactMode;
    try {
      seriesId = jsonObject.getJSONObject("data").getJSONObject("id").getInt("seriesId");
    } catch (JSONException e) {
      throw new RuntimeException(e);
    } 
  }
  
  @Override
  public boolean clicked(Activity activity) {
    // Click only works in compact mode
    if (compactMode) {
      try {
        Intent intent = new Intent(activity, SeriesActivity.class);
        intent.putExtra("seriesId", seriesId);
        intent.putExtra("name", jsonObject.getJSONObject("data").getString("name"));
        activity.startActivity(intent);
      } catch (JSONException e) {
        throw new RuntimeException(e);
      }
    }
    return true;
  }

  public void customizeView(View view, Activity activity) {
    try {
      JSONObject data = jsonObject.getJSONObject("data");
      ((TextView) view.findViewById(R.id.Name)).setText(data.getString("name"));
      if (!compactMode) {
        TextView descriptionTextView = (TextView) view.findViewById(R.id.Description);
        String description = data.getString("description");
        if ("".equals(description)) {
          descriptionTextView.setVisibility(View.GONE);
        } else {
          descriptionTextView.setVisibility(View.VISIBLE);
          descriptionTextView.setText(description);
        }
      }
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }

  public int getLayoutId() {
    return compactMode ? R.layout.series_entry_compact : R.layout.series_entry;
  }

  @Override
  protected String getUriString() {
    String e = Integer.toString(seriesId, 16);
    return "http://www.google.com/moderator/#16/e=" + e;
  }
}
